using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CreamX.Framework.Game;
using CreamX.Framework.Game.Components;

namespace CreamXStudio.Screens
{
    public partial class TileEditor : EditorControlBase
    {
        public TileEditor()
        {
            InitializeComponent();

            tileSetRenderer1.MouseDown += new MouseEventHandler(tileSetRenderer1_MouseDown);
            tileSetRenderer1.MouseUp += new MouseEventHandler(tileSetRenderer1_MouseUp);
            tileSetRenderer1.MouseMove += new MouseEventHandler(tileSetRenderer1_MouseMove);
            
        }

        private void LoadMaterials()
        {
            //cboMaterials.Items.Clear();
            //foreach (CreamXMaterialBase mat in TileGrid.Scene.Materials)
            //{
            //    cboMaterials.Items.Add(mat.Name);
            //}
        }

        void tileSetRenderer1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                switch (currentTool)
                {
                    case ToolType.Paint:
                        DoPaint(new Point(e.X, e.Y));
                        break;
                    case ToolType.Delete:
                        DoDelete(new Point(e.X, e.Y));
                        break;
                }
            }
        }

        private void DoDelete(Point point)
        {
            int i = -1;

            foreach (CreamXStudio.XNARenderer.TileSetRenderer.Cell var in tileSetRenderer1.Cells)
            {
                if (var.Equals(tileSetRenderer1.HighlightedCell))
                {
                    i = tileSetRenderer1.Cells.IndexOf(var);
                    break;
                }
            }
            if (i > -1)
            {
                if (_selectedLayer.InternalData[i] != null && i > -1)
                {
                    _selectedLayer.InternalData[i] = "-1";
                    comp.LoadData(false);
                }
                //if (comp.TileDataInt != null)
                //{
                //  comp.TileDataInt[i] = selected;
                //                comp.Tiles[i].TileNumber = selected;
                //              comp.Tiles[i].SourceRectangle = tilesControl1.SourceRect;
                //comp.LoadDataInt();
                //}
            }
            tileSetRenderer1.Invalidate();
        }

        private void DoPaint(Point point)
        {
            int i = -1;
            foreach (CreamXStudio.XNARenderer.TileSetRenderer.Cell var in tileSetRenderer1.Cells)
            {
                if (var.Equals(tileSetRenderer1.HighlightedCell))
                {
                    i = tileSetRenderer1.Cells.IndexOf(var);
                    break;
                }
            }
            
            int selected=tilesControl1.SelectedTile;
            if (selected > -1 && i > -1)
            {
                if (_selectedLayer!= null)
                {
                    _selectedLayer.InternalData[i] = selected.ToString();
                    //comp._internalData[i] = selected.ToString();
                    if (radrot90.Checked)
                        _selectedLayer.InternalData[i] += "(1)";
                    if (radrot180.Checked)
                        _selectedLayer.InternalData[i] += "(2)";
                    if (radrot270.Checked)
                        _selectedLayer.InternalData[i] += "(3)";

                    comp.LoadData(false);
                }
                //if (comp.TileDataInt != null)
                //{
                  //  comp.TileDataInt[i] = selected;
                    //                comp.Tiles[i].TileNumber = selected;
                    //              comp.Tiles[i].SourceRectangle = tilesControl1.SourceRect;
                    //comp.LoadDataInt();
                //}
            }
                tileSetRenderer1.Invalidate();
        }

        void tileSetRenderer1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                mouseDown = false;
        }
        bool mouseDown = false;
        void tileSetRenderer1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDown = true;
                switch (currentTool)
                {
                    case ToolType.Paint:
                        DoPaint(new Point(e.X, e.Y));
                        break;
                    case ToolType.Delete:
                        DoDelete(new Point(e.X, e.Y));
                        break;
                }
            }
        }

        Rectangle position = Rectangle.Empty;
      
        CreamXTileGrid TileGrid;
        CreamXTileGridComponent comp;
        internal override void SetData(object data)
        {
            TileGrid = data as CreamXTileGrid;
            comp = TileGrid.GetComponent<CreamXTileGridComponent>();
            tileSetRenderer1.TileGrid = comp;
            tilesControl1.tileset = comp;
            trackBar1.Value = (int)(tileSetRenderer1.TileGrid.SceneObject.Scene.Camera.Zoom*20f);
            LoadMaterials();
            
            
            if (TileGrid.Material != null)
            {
                if(comp.TileCutSizeX==0)
                    tilesControl1.SetScale(1);
                else
                    tilesControl1.SetScale((float)(64f / (float)comp.TileCutSizeX));
            }
            

            RefreshLayers();
            
            base.SetData(data);

        }


        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            tilesControl1.Invalidate();
        }
        public ToolType currentTool = ToolType.Paint;

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            
        }

        private void txtCountX_TextChanged(object sender, EventArgs e)
        {
          
        }

        private void txtCountY_TextChanged(object sender, EventArgs e)
        {
          
        }

        private void txtCutX_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void txtCutY_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtDisplayX_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtDisplayY_TextChanged(object sender, EventArgs e)
        {

        }


        private void cboMaterials_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            UpdateButtons(ToolType.Delete);
        }

        private void UpdateButtons(ToolType type)
        {
            currentTool = type;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            switch (type)
            {
                case ToolType.Paint:
                    toolStripButton1.Checked = true;
                    break;
                case ToolType.Delete:
                    toolStripButton2.Checked = true;
                    break;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            UpdateButtons(ToolType.Paint);
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void lstLayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        TileLayer _selectedLayer;
        private void lstLayers_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            ((TileLayer)lstLayers.Items[e.Index]).Visible = (e.NewValue==CheckState.Checked)  ?  true : false;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            toolStripButton3.Checked = !toolStripButton3.Checked;
            
        }

        private void tileSetRenderer1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            TileLayer tl = new TileLayer();
            tl.Name = "TileLayer";
            tl.Order = lstLayers.Items.Count;
            tl.Visible = true;
            comp.ResetTileLayerData(tl);
            comp.TileLayers.Add(tl);
            RefreshLayers();
        }

        private void RefreshLayers()
        {
            lstLayers.Items.Clear();
            foreach (TileLayer layer in comp.TileLayers)
            {
                lstLayers.Items.Add(layer, layer.Visible);
            }
            if(lstLayers.Items.Count>0)
                lstLayers.SelectedItem = lstLayers.Items[0];
        }

        private void lstLayers_DoubleClick(object sender, EventArgs e)
        {
            if (lstLayers.SelectedItem != null)
            {
                TileNameChange frm = new TileNameChange(_selectedLayer.Name);
                frm.ShowDialog();
                _selectedLayer.Name = frm.LayerName;
                RefreshLayers();
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            System.Drawing.Color ci = new Color();
            colorDialog1.Color = Color.FromArgb(TileGrid.Color.R,
                TileGrid.Color.G, TileGrid.Color.B, TileGrid.Color.A);
            colorDialog1.ShowDialog();
            TileGrid.Color = new Microsoft.Xna.Framework.Graphics.Color(colorDialog1.Color.R,
                colorDialog1.Color.G, colorDialog1.Color.B, colorDialog1.Color.A);
        }

        private void lstLayers_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            _selectedLayer = (TileLayer)lstLayers.SelectedItem;
        }

        private void trackBar1_Scroll_1(object sender, EventArgs e)
        {
            tileSetRenderer1.TileGrid.SceneObject.Scene.Camera.Zoom = (float)trackBar1.Value / 20f;
        }

        
    }
    public enum ToolType
    {
        Paint,
        Fill,
        Delete
    }
}
